package service.impl;

import dao.impl.CollectDaoImpl;
import dao.impl.ConcernDaoImpl;
import dao.impl.FileDaoImpl;
import dao.impl.InformMessageDaoImpl;
import domian.*;
import service.CollectService;
import utils.ToolUtil;

import java.util.Date;
import java.util.List;

/**
 * 收藏业务类
 */
public class CollectServiceImpl implements CollectService {
    CollectDaoImpl collectDao= new CollectDaoImpl();
    ConcernDaoImpl concernDao = new ConcernDaoImpl();
    InformMessageDaoImpl informMessageDao = new InformMessageDaoImpl();
    FileDaoImpl fileDao = new FileDaoImpl();
    @Override
    public Result CollectMessage(Collect collect,String coll,String nickname) {
        Result result = new Result();
        ResultStatus  collectSuccess=null;
        Integer integer =null;
        //拿到文件的名字
        File fileById = fileDao.findFileById(collect.getFid());
        String fTitle = fileById.getF_title();
       if (coll.equals("收藏")){
           integer = collectDao.addCollect(collect); //添加收藏
           Concern concerns = concernDao.viewUid(collect.getFid()); //调用拿到用户的id，先查询到被关注用户的id
           Integer focusUid = concerns.getFocus_uid();
           String nicknames ="【收藏】"+nickname+"收藏了你的资源("+fTitle+")";
           informMessageDao.addInform(new InformMessage(focusUid,nicknames, ToolUtil.dateString(new Date())));
       }else {
           integer = collectDao.cancelCollect(collect); //取消收藏
           Concern concerns = concernDao.viewUid(collect.getFid()); //调用拿到用户的id，先查询到被关注用户的id
           Integer focusUid = concerns.getFocus_uid();
           String nicknames ="【取消收藏】"+nickname+"取消收藏了你的资源("+fTitle+")";
           informMessageDao.addInform(new InformMessage(focusUid,nicknames, ToolUtil.dateString(new Date())));
       }
        if (integer>0){
            //收藏/取消收藏成功
             collectSuccess = ResultStatus.COLLECT_SUCCESS;
        }else {
            //收藏、取消收藏失败
            collectSuccess  = ResultStatus.COLLECT_ERROR;
        }
        result.setCode(collectSuccess.getCode());
        result.setMsg(collectSuccess.getMsg());
        return result;
  }

    @Override
    public PageBean viewFavorites( Integer uid,Integer pages) {
       //先传一次空，查找总条数
        List<Collect> coll= collectDao.viewCollect(uid, null, null);
        //获取总条数
        Integer size = coll.size();
       /* System.out.println("总体条数： "+size);*/

        List<Collect> collects = collectDao.viewCollect(uid, (pages-1)*5,5);

        //获取总页数
        double ceil = Math.ceil((double) size / 5);
        Integer ce = (int) ceil;
       /* System.out.println("总页数： "+ce);*/

        PageBean pageBean = new PageBean(size,ce,collects);

        return pageBean;
    }

    @Override
    public PageBean fidCollectService(Integer uid) {
        List<Collect> collects = collectDao.fidCollect(uid);
        PageBean pageBean = new PageBean(collects);
        return pageBean;
    }
}
